//
//  ViewController.m
//  EWPTest2
//
//  Created by Dirk Jacobasch on 22.12.11.
//  Copyright (c) 2011 NEURONprocessing. All rights reserved.
//

#import "SampleViewController.h"
#import "InsuranceTabbarController.h"
#import <QuartzCore/QuartzCore.h>

#define EWP_BANNER_FEUCHT_IPHONE @"EWP_Banner_Feucht_iPhone"
#define EWP_BANNER_FEUCHT_IPOD @"EWP_Banner_Feucht_iPhone"

#define EWP_CONFIRMATION @"EWP_Bestaetigung"

@implementation SampleViewController

@synthesize bannerView;
@synthesize addView;
@synthesize addButton;
@synthesize ewpLib;
@synthesize moviePlayer;
@synthesize player;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    [self.addView setHidden:YES];
	[self.addButton setEnabled:NO];
	[self.addButton setHidden:YES];
    
    // Create library object
    self.ewpLib = [[[EWPLibrary alloc] init] autorelease];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // establish which device type the app is running on, so as to choose the right movie, or in the case of iPad not show a movie nor activate the button
	NSString *aDeviceModel = [[UIDevice currentDevice] model];
	
	NSURL *url = [NSURL URLWithString:@""];
	
	[self.addView setHidden:NO];
	[self.addButton setEnabled:YES];
	[self.addButton setHidden:NO];
	
    // Set the movie for the banner here
	if ([aDeviceModel rangeOfString:@"iPhone"].location != NSNotFound) {
		url = [[NSBundle mainBundle] URLForResource:EWP_BANNER_FEUCHT_IPHONE withExtension:@"m4v"];
        
	} else if ([aDeviceModel rangeOfString:@"iPod"].location != NSNotFound) {
		url = [[NSBundle mainBundle] URLForResource:EWP_BANNER_FEUCHT_IPOD withExtension:@"m4v"];
        
	} else if ([aDeviceModel rangeOfString:@"iPad"].location != NSNotFound) {
		[self.addView setHidden:YES];
		[self.addButton setEnabled:NO];
		[self.addButton setHidden:YES];
	}
	
    // #################################################################################
    // Dont forget to set the agent number, your username and password.
    // #################################################################################
	Insurance *insuranceData = [[Insurance alloc] initWithAgentNumber:@"234-324" andUsername:@"myUsername" andPassword:@"myPassword"];
	
	if ([aDeviceModel rangeOfString:@"iPad"].location == NSNotFound) {
		if (insuranceData.insured) {
			url = [[NSBundle mainBundle] URLForResource:EWP_CONFIRMATION withExtension:@"m4v"];
		}
		
		player = [[MPMoviePlayerController alloc] initWithContentURL:url];
        
        // Change banner position
        //self.bannerView.frame = CGRectMake(0.0f, 150.0f, self.addView.frame.size.width, self.addView.frame.size.height);
        
        self.player.view.frame = [self.addView bounds];
        self.addButton.frame = self.addView.frame;
        
		[self.addView addSubview: [self.player view]];
		
		[self.player setControlStyle:MPMovieControlStyleNone];
		[self.player setRepeatMode:MPMovieRepeatModeOne];
		[self.player setShouldAutoplay:YES];
		self.moviePlayer = self.player;
		
		[self.moviePlayer play];
	}
    [insuranceData release];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

// Stop the movie
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    if ([[[UIDevice currentDevice] model] rangeOfString:@"iPad"].location == NSNotFound) {
		[self.moviePlayer stop];
		self.moviePlayer = nil;
	}
}

// Handle banner button events
- (IBAction)openEWP:(id)sender {
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
	[self setWantsFullScreenLayout:YES];
    [self.ewpLib loadInsuranceViewWithViewController:self];
}

/*
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
    // Return YES for supported orientations
    return UIInterfaceOrientationPortraitUpsideDown;
}
*/

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    self.bannerView = nil;
    self.addView = nil;
    self.addButton = nil;
}

- (void)dealloc {
    [bannerView release];
    [addView release];
    [addButton release];
    [ewpLib release];
    [moviePlayer release];
    [player release];
    [super dealloc];
}

@end
